#include <iostream>
#include "fraction.h"
#include <cstdlib>

fraction::fraction(long long p, long long q)
{
    if(q==0) {std::cerr<<"Fatal: Q value must be different than 0. Quitting"<<std::endl; exit(-1);}
    this->p=p;
    this->q=q;
    this->simplify();
}

void fraction::simplify()
{
    long long nwd=NWD(this->p,this->q);
    this->p/=nwd;
    this->q/=nwd;
    if(this->q<0) {q*=(-1);p*=(-1);}
}

void fraction::print()
{   if(q==1) std::cout<<p;
    else std::cout<<p<<"/"<<q;
}

fraction fraction::operator+(fraction a)
{
    long long p,q;
    q=NWW(this->q,a.q);
    p=(this->p*q/this->q)+(a.p*q/a.q);
    return fraction(p,q);
}

fraction fraction::operator-(fraction a)
{
    long long p;
    p=a.p*(-1);
    return fraction(this->p,this->q)+fraction(p,a.q);
}

fraction fraction::operator*(fraction a)
{
    return fraction(this->p*a.p,this->q*a.q);
}

fraction fraction::operator*(long long a)
{
	fraction b(a,1);
    return fraction(this->p*b.p,this->q*b.q);
}

fraction fraction::operator/(fraction a)
{
    long long p,q;
    p=a.q;
    q=a.p;
    return fraction(p,q)*fraction(this->p,this->q);
}

double fraction::fr_sqrt()
{
return sqrt(p/q);
}
